﻿using RTracker.Models.Common;
using RTracker.Areas.Recruitment.Models.VacancySalary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RTracker.Areas.Recruitment.Models.Client;

namespace RTracker.Areas.Recruitment.Controllers
{
    public class VacancySalaryController : RTracker.Controllers.AppBaseController
    {
        //
        // GET: /VacancySalary/

        [HttpPost]
        public ActionResult Save(GVacancySalary oGVacancySalary)
        {
            try
            {
                //check if Model validation is true if not then return detail view with errors
                if (true)//ModelState.IsValid
                {
                    //call CheckSession to get session status
                    string SessionState = CheckSessionState();
                    //check if session state is ok else return false status to view.
                    if (SessionState == CAppConstants.SessionSuccess)
                    {
                        bool _userPermission = false;

                        if (oGVacancySalary.VacancySalary.OrganizationId == 0)
                        {
                            _userPermission = checkUserPermission(CUserPermissionsConstants.OrganizationCreate);
                        }
                        else
                        {
                            _userPermission = checkUserPermission(CUserPermissionsConstants.OrganizationUpdate);
                        }


                        if (_userPermission)
                        {
                            CVacancySalary objCVacancySalary = new CVacancySalary();
                            objCVacancySalary = oGVacancySalary.VacancySalary;
                            MVacancySalary objMVacancySalary = new MVacancySalary();

                            if (objCVacancySalary.VacancySalaryId == 0)
                            {
                                objCVacancySalary.OrganizationId = CurrentUserSession.OrganizationId;
                                oGVacancySalary.Status = objMVacancySalary.CreateVacancySalary(AuthenticationContext, ref objCVacancySalary);
                            }
                            else
                            {
                                oGVacancySalary.Status = objMVacancySalary.UpdateVacancySalary(AuthenticationContext, ref objCVacancySalary);
                            }
                        }
                        else
                        {
                            oGVacancySalary.Status = new CStatus();
                            oGVacancySalary.Status.Success = false;
                            oGVacancySalary.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        oGVacancySalary.Status = new CStatus();
                        oGVacancySalary.Status.Success = false;
                        oGVacancySalary.Status.StatusDetails = SessionState;
                    }
                }
                else
                {
                    oGVacancySalary.Status = new CStatus();
                    oGVacancySalary.Status.Success = false;
                    oGVacancySalary.Status.StatusDetails = CAppErrorsConstants.ValidationFail;
                }
            }
            catch (Exception ex)
            {
                oGVacancySalary.Status = new CStatus();
                oGVacancySalary.Status.Success = false;
                oGVacancySalary.Status.StatusDetails = ex.Message;
            }
            return Json(oGVacancySalary);
        }

        [HttpPost]
        public ActionResult GetDetails(int pnId)
        {
            GVacancySalary objGVacancySalary = new GVacancySalary();

            objGVacancySalary.Status = new CStatus();

            objGVacancySalary.VacancySalary = new CVacancySalary();

            objGVacancySalary.VacancySalary.VacancySalaryId = pnId;

            CVacancySalary objCVacancySalary = new CVacancySalary();

            ViewBag.IsDisabled = true;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();

                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    MClient objMClient = new MClient();

                    // get Client list
                    List<CClient> tempListClient = new List<CClient>();
                    objGVacancySalary.Status = objMClient.GetClientList(AuthenticationContext, CurrentUserSession.OrganizationId, out tempListClient);
                    objGVacancySalary.LstClient = new List<CClient>();
                    objGVacancySalary.LstClient.AddRange(tempListClient);

                    //check if Id param is 0 means new is clicked then set selected row object to new.
                    if (0 == pnId)
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationCreate))
                        {
                            //here we will append new form to create new organisation
                            ViewBag.IsNew = true;
                            objGVacancySalary.Status.Success = true;
                            objGVacancySalary.Status.StatusDetails = CAppConstants.DetailSuccess;
                        }
                        else
                        {
                            objGVacancySalary.Status.Success = false;
                            objGVacancySalary.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationDetailView))
                        {
                            //here we will append new for detail 
                            MVacancySalary objMVacancySalary = new MVacancySalary();

                            ViewBag.IsNew = false;

                            objCVacancySalary.VacancySalaryId = objGVacancySalary.VacancySalary.VacancySalaryId;

                            objGVacancySalary.Status = objMVacancySalary.GetVacancySalaryDetail(AuthenticationContext, ref objCVacancySalary);

                            //get the different admin names from their id from the application VacancySalary list
                            objCVacancySalary.CreatedByName = GetFullName(objCVacancySalary.CreatedBy);
                            objCVacancySalary.ModifiedByName = GetFullName(objCVacancySalary.ModifiedBy);
                            objCVacancySalary.SoftLockedByName = GetFullName(objCVacancySalary.SoftLockedBy);
                            objCVacancySalary.HardLockedByName = GetFullName(objCVacancySalary.HardLockedBy);


                            if (!CheckLocksByOther(objCVacancySalary))
                            {
                                ViewBag.IsDisabled = false;
                            }
                            if (objGVacancySalary.Status.Success == true)
                            {
                                objGVacancySalary.VacancySalary = objCVacancySalary;
                            }
                        }
                        else
                        {
                            objGVacancySalary.Status = new CStatus();
                            objGVacancySalary.Status.Success = false;
                            objGVacancySalary.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                }
                else
                {
                    objGVacancySalary.Status = new CStatus();
                    objGVacancySalary.Status.Success = false;
                    objGVacancySalary.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancySalary.Status = new CStatus();
                objGVacancySalary.Status.Success = false;
                objGVacancySalary.Status.StatusDetails = ex.Message;
            }

            return PartialView("_detail", objGVacancySalary);
        }

        [HttpPost]
        public ActionResult DeleteRecords(string pstrIds)
        {
            GVacancySalary objGVacancySalary = new GVacancySalary();

            objGVacancySalary.VacancySalary = new CVacancySalary();

            objGVacancySalary.VacancySalary.VacancySalaryIds = pstrIds;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();

                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;

                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    //check if Id param is 0 means new is clicked then set selected row object to new.
                    if (pstrIds != "")
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationDelete))
                        {
                            //code to delete the record
                            MVacancySalary objMVacancySalary = new MVacancySalary();

                            CVacancySalary objCVacancySalary = new CVacancySalary();

                            objCVacancySalary.VacancySalaryIds = objGVacancySalary.VacancySalary.VacancySalaryIds;

                            objGVacancySalary.Status = objMVacancySalary.DeleteVacancySalary(AuthenticationContext, ref objCVacancySalary);

                            if (objGVacancySalary.Status.Success == true)
                            {
                                objGVacancySalary.VacancySalary = objCVacancySalary;
                            }
                        }
                        else
                        {
                            objGVacancySalary.Status = new CStatus();
                            objGVacancySalary.Status.Success = false;
                            objGVacancySalary.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        objGVacancySalary.Status = new CStatus();
                        objGVacancySalary.Status.Success = false;
                        objGVacancySalary.Status.StatusDetails = CAppErrorsConstants.NotValidRequest;
                    }
                }
                else
                {
                    objGVacancySalary.Status = new CStatus();
                    objGVacancySalary.Status.Success = false;
                    objGVacancySalary.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancySalary.Status = new CStatus();
                objGVacancySalary.Status.Success = false;
                objGVacancySalary.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancySalary.Status);
        }

        [HttpPost]
        public ActionResult GridRefresh(int pnId, string pstrSortOn, string pstrSortType, int pnPageNo, int pnPageSize, string pstrSearch, string pstrSearchOn, int pnOrgId = 1)
        {

            GVacancySalary objGVacancySalary = new GVacancySalary();
            try
            {
                if (pstrSearch == null || pstrSearchOn == null)
                {
                    pstrSearch = "";
                    pstrSearchOn = "";
                }

                //call CheckSession to get session status
                string SessionState = CheckSessionState();

                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    CVacancySalary objCVacancySalary = new CVacancySalary() { VacancySalaryId = pnId, SortOn = pstrSortOn, SortType = pstrSortType, PageNumber = pnPageNo, RowSize = pnPageSize, Search = pstrSearch, SearchOn = pstrSearchOn, OrganizationId = pnOrgId };


                    //check if current users organization id is 1 then it means it is superUser then need to get all organizations list
                    if (CurrentUserSession.OrganizationId == pnOrgId)
                    {

                        MVacancySalary objMVacancySalary = new MVacancySalary();

                        objGVacancySalary = objMVacancySalary.GetVacancySalaryGridList(objCVacancySalary, AuthenticationContext);

                        if (objGVacancySalary.lstVacancySalary != null)
                        {
                            if (objGVacancySalary.lstVacancySalary.Count > 0)
                            {
                                objGVacancySalary.lstVacancySalary.ForEach(VacancySalary =>
                                {
                                    VacancySalary.CreatedByName = GetFullName(VacancySalary.CreatedBy);
                                    VacancySalary.ModifiedByName = GetFullName(VacancySalary.ModifiedBy);
                                    VacancySalary.SoftLockedByName = GetFullName(VacancySalary.SoftLockedBy);
                                    VacancySalary.HardLockedByName = GetFullName(VacancySalary.HardLockedBy);
                                });
                            }
                        }

                        // if status is true get object of the record to be selected in the list.
                        // first of all we will check the if given record exist in the list.
                        // if given record exist then get the object of that record.
                        // if record not exist in the list then select the first record of the list.
                        if (objGVacancySalary.Status.Success)
                        {
                            // if status is true get object of the record to be selected in the list.
                            // first of all we will check the if given record exist in the list.
                            // if given record exist then get the object of that record.
                            // if record not exist in the list then select the first record of the list.
                            if (objGVacancySalary.lstVacancySalary != null)
                            {
                                CVacancySalary objSelectedCVacancySalary = objGVacancySalary.lstVacancySalary.Where(m => m.VacancySalaryId == pnId).DefaultIfEmpty(objGVacancySalary.lstVacancySalary[0]).First();

                                // check if record is not already hard locked or softlocked
                                if (objSelectedCVacancySalary.IsSoftLocked == false && objSelectedCVacancySalary.IsHardLocked == false)
                                {
                                    objSelectedCVacancySalary.CreatedBy = CurrentUserSession.UserId;
                                    objGVacancySalary.Status = objMVacancySalary.SetVacancySalarySoftLock(AuthenticationContext, objSelectedCVacancySalary);
                                    if (objGVacancySalary.Status.Success)
                                    {
                                        // updating the list with softlock details
                                        objGVacancySalary.lstVacancySalary.Where(m => m.VacancySalaryId == objSelectedCVacancySalary.VacancySalaryId).First().IsSoftLocked = true;
                                        objGVacancySalary.lstVacancySalary.Where(m => m.VacancySalaryId == objSelectedCVacancySalary.VacancySalaryId).First().SoftLockedBy = CurrentUserSession.UserId;
                                    }
                                }
                            }


                        }

                        ViewBag.bPagingEnabled = true;
                        ViewBag.bSearchEnabled = true;
                        ViewBag.bNewDeleteEnabled = true;
                        ViewBag.bCopyPasteEnabled = false;
                    }
                    //else get detail for current users organization and show it in grid.
                    else
                    {
                        objCVacancySalary = new CVacancySalary() { OrganizationId = CurrentUserSession.OrganizationId };

                        MVacancySalary objMVacancySalary = new MVacancySalary();

                        objGVacancySalary.Status = objMVacancySalary.GetVacancySalaryDetail(AuthenticationContext, ref objCVacancySalary);

                        objGVacancySalary.lstVacancySalary.Add(objCVacancySalary);
                        //set view bag variables for pagging, new, delete and search.
                        ViewBag.bPagingEnabled = false;
                        ViewBag.bSearchEnabled = false;
                        ViewBag.bNewDeleteEnabled = false;
                        ViewBag.bCopyPasteEnabled = false;
                    }

                    // check if status is true
                    if (objGVacancySalary.Status.Success)
                    {
                        if (objGVacancySalary.lstVacancySalary != null)
                        {
                            if (objGVacancySalary.lstVacancySalary.Count > 0)
                            {
                                objGVacancySalary.lstVacancySalary[0].SortOn = pstrSortOn;
                                objGVacancySalary.lstVacancySalary[0].SortType = pstrSortType;
                            }
                        }
                        // set the values of sort on and sort type in the view bag.
                        ViewBag.SortType = pstrSortType;
                        ViewBag.SortOn = pstrSortOn;
                        ViewBag.CurrentUser = CurrentUserSession.UserId;
                    }
                }
                else
                {
                    //set status to false and its detail.
                    objGVacancySalary.Status = new CStatus();
                    objGVacancySalary.Status.Success = false;
                    objGVacancySalary.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {
                // catch the exceptions in any step ,if any 
                //set status to false and its detail as exception message.
                objGVacancySalary.Status = new CStatus();
                objGVacancySalary.Status.Success = false;
                objGVacancySalary.Status.StatusDetails = ex.Message;
            }
            ViewBag.CurrentUser = CurrentUserSession.UserId;
            //check if status is true then return list view(partial view of list) else return object with false status.
            if (objGVacancySalary.Status.Success)
                return PartialView("_list", objGVacancySalary);
            else
                return Json(objGVacancySalary);
        }

        [HttpPost]
        public ActionResult ReleaseSoftLock(int pnId)
        {
            GVacancySalary objGVacancySalary = new GVacancySalary();

            objGVacancySalary.VacancySalary = new CVacancySalary();

            objGVacancySalary.VacancySalary.VacancySalaryId = pnId;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();
                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {

                    //check if Id param is 0 means new is clicked then set selected row object to new.
                    if (0 == pnId)
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationUpdate))
                        {
                            //here we will append new form to create new organisation
                            ViewBag.IsNew = true;
                            objGVacancySalary.Status.Success = true;
                        }
                        else
                        {
                            objGVacancySalary.Status.Success = false;
                            objGVacancySalary.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        MVacancySalary objMVacancySalary = new MVacancySalary();
                        objGVacancySalary.VacancySalary.CreatedBy = CurrentUserSession.UserId;
                        objGVacancySalary.Status = objMVacancySalary.ReleaseVacancySalarySoftLock(AuthenticationContext, objGVacancySalary.VacancySalary);
                    }
                }
                else
                {
                    objGVacancySalary.Status = new CStatus();
                    objGVacancySalary.Status.Success = false;
                    objGVacancySalary.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancySalary.Status = new CStatus();
                objGVacancySalary.Status.Success = false;
                objGVacancySalary.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancySalary.Status);
        }

        [HttpPost]
        public ActionResult SetHardLock(int pnId)
        {
            GVacancySalary objGVacancySalary = new GVacancySalary();

            objGVacancySalary.VacancySalary = new CVacancySalary();

            objGVacancySalary.VacancySalary.VacancySalaryId = pnId;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();
                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    MVacancySalary objMVacancySalary = new MVacancySalary();
                    objGVacancySalary.VacancySalary.CreatedBy = CurrentUserSession.UserId;
                    objGVacancySalary.Status = objMVacancySalary.SetVacancySalaryHardLock(AuthenticationContext, objGVacancySalary.VacancySalary);
                }
                else
                {
                    objGVacancySalary.Status = new CStatus();
                    objGVacancySalary.Status.Success = false;
                    objGVacancySalary.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancySalary.Status = new CStatus();
                objGVacancySalary.Status.Success = false;
                objGVacancySalary.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancySalary.Status);
        }

        [HttpPost]
        public ActionResult ReleaseHardLock(int pnId)
        {
            GVacancySalary objGVacancySalary = new GVacancySalary();

            objGVacancySalary.VacancySalary = new CVacancySalary();

            objGVacancySalary.VacancySalary.VacancySalaryId = pnId;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();
                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    MVacancySalary objMVacancySalary = new MVacancySalary();
                    objGVacancySalary.VacancySalary.CreatedBy = CurrentUserSession.UserId;
                    objGVacancySalary.Status = objMVacancySalary.ReleaseVacancySalaryHardLock(AuthenticationContext, objGVacancySalary.VacancySalary);
                }
                else
                {
                    objGVacancySalary.Status = new CStatus();
                    objGVacancySalary.Status.Success = false;
                    objGVacancySalary.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancySalary.Status = new CStatus();
                objGVacancySalary.Status.Success = false;
                objGVacancySalary.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancySalary.Status);
        }

    }
}
